package com.hzy.fivepalace.common.express;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.google.gson.Gson;
import com.hzy.fivepalace.common.util.ResponseUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import com.hzy.fivepalace.common.express.config.ExpressProperties;
import com.hzy.fivepalace.common.express.dao.ExpressInfo;

/**
 * 物流查询服务
 * <p>
 * 快递100接口文档 https://api.kuaidi100.com/document/5f0ffb5ebc8da837cbd8aefc
 */
public class ExpressService {
    // 请求url
    private String ReqURL = "https://poll.kuaidi100.com/poll/query.do";

    private ExpressProperties properties;

    public ExpressProperties getProperties() {
        return properties;
    }

    public void setProperties(ExpressProperties properties) {
        this.properties = properties;
    }


    /**
     * 获取物流供应商名
     *
     * @param vendorCode
     * @return
     */
    public String getVendorName(String vendorCode) {
        for (Map<String, String> item : properties.getVendors()) {
            if (item.get("code").equals(vendorCode))
                return item.get("name");
        }
        return null;
    }

    /**
     * 获取所有快递公司
     *
     * @return
     */
    public List<Map<String, String>> getAllShipChannel() {
        return properties.getVendors();
    }




    /**
     * 获取物流信息
     * @param expCode
     * @param expNo
     * @param phone
     * @return
     */
    public Object getExpressInfo(String expCode, String expNo,String phone) {

        ExpressInfo expressInfo = new ExpressInfo();
        //获取快递公司名称
        expressInfo.setShipperName(getVendorName(expCode));
        expressInfo.setShipperSn(expNo);

        if (!properties.isEnable()) {
            return ResponseUtil.fail("获取物流信息失败,功能已禁用");
        }
        Map<String, Object> param = new HashMap<>();
        param.put("com", expCode);
        param.put("num", expNo);
        if(!StringUtils.isBlank(phone)){
            param.put("phone", phone);
        }
        String paramStr = new Gson().toJson(param);
        String result = HttpRequest.post(String.format("%s?customer=%s&sign=%s&param=%s", ReqURL, properties.getAppKey(), getSign(paramStr), paramStr))
                .timeout(60000)
                .contentType("application/x-www-form-urlencoded")
                .execute().body();
        if (StringUtils.isBlank(result)) {
            return ResponseUtil.fail("获取物流信息失败,无返回结果");
        }
        Map<String, Object> response = new HashMap<>();
        try {
            response = JSONObject.parseObject(result, Map.class);
            if (MapUtils.getIntValue(response, "status") != 200) {
                return ResponseUtil.fail("获取物流信息失败," + MapUtils.getString(response, "message"));
            }
            expressInfo.setDataList(JSONArray.parseArray(MapUtils.getString(response, "data")));
            return ResponseUtil.ok(expressInfo);
        } catch (Exception e) {
            return ResponseUtil.fail("获取物流信息失败," + e.getMessage());
        }

    }


    /**
     * sign签名生成
     *
     * @return
     */
    private String getSign(String paramStr) {
        return DigestUtils.md5Hex(paramStr + properties.getAppId() + properties.getAppKey()).toUpperCase();
    }
    
}
